#include "Vector2.h"
#include <cmath>

using namespace vn;

const Vector2 Vector2::ZERO(0.0f, 0.0f);
const Vector2 Vector2::UNIT_X(1.0f, 0.0f);
const Vector2 Vector2::UNIT_Y(0.0f, 1.0f);
const Vector2 Vector2::NEGATIVE_UNIT_X(-1.0f, 0.0f);
const Vector2 Vector2::NEGATIVE_UNIT_Y(0.0f, -1.0f);

Vector2::Vector2() : x(0.0f), y(0.0f){}

Vector2::Vector2(float p_x, float p_y) : x(p_x), y(p_y){}
Vector2::Vector2(int p_x, int p_y) : x(static_cast<float>(p_x)), y(static_cast<float>(p_y)){}

Vector2::Vector2(const Vector2& p_vector2)
{
	x = p_vector2.x;
	y = p_vector2.y;
}

Vector2::~Vector2()
{
}

Vector2& Vector2::operator= (const Vector2& rhs)
{
	x = rhs.x;
	y = rhs.y;

	return *this;
}

bool Vector2::operator== (const Vector2& rhs) const
{
	if (x == rhs.x && y == rhs.y)
		return true;

	return false;
}

bool Vector2::operator!= (const Vector2& rhs) const
{
	if (x != rhs.x || y != rhs.y)
		return true;

	return false;
}

Vector2 Vector2::operator* (const Vector2& rhs) const
{
	Vector2 result;
	result.x = x * rhs.x;
	result.y = y * rhs.y;

	return result;
}

Vector2 Vector2::operator+ (const Vector2& rhs) const
{
	Vector2 result;
	result.x = x + rhs.x;
	result.y = y + rhs.y;

	return result;
}

Vector2 Vector2::operator- (const Vector2& rhs) const
{
	Vector2 result;
	result.x = x - rhs.x;
	result.y = y - rhs.y;

	return result;
}

Vector2 Vector2::operator/ (const Vector2& rhs) const
{
	Vector2 result;
	result.x = x / rhs.x;
	result.y = y / rhs.y;

	return result;
}

Vector2 Vector2::operator * (const float fScalar) const
{
	Vector2 result;
	result.x = x * fScalar;
	result.y = y * fScalar;

	return result;
}

Vector2 Vector2::operator / (const float fScalar) const
{
	Vector2 result;
	result.x = x / fScalar;
	result.y = y / fScalar;

	return result;
}

Vector2 Vector2::operator + (const float fScalar) const
{
	Vector2 result;
	result.x = x + fScalar;
	result.y = y + fScalar;

	return result;
}

Vector2 Vector2::operator - (const float fScalar) const
{
	Vector2 result;
	result.x = x - fScalar;
	result.y = y - fScalar;

	return result;
}

float Vector2::magnitude() const
{
	return sqrtf( (x * x) + (y * y) );
}

float Vector2::squaredMagnitude() const
{
	return (x * x) + (y * y);
}

void Vector2::normalize()
{
	float length( 1.0f / sqrtf( (x * x ) + (y * y) ) );

	x *= length;
	y *= length;
}

Vector2 Vector2::normalizedCopy() const
{
	float length( 1.0f / sqrtf( (x * x ) + (y * y) ) );
	Vector2 result;
	result.x *= length;
	result.y *= length;

	return result;
}

float Vector2::dot(const Vector2& rhs) const
{
	return (x * rhs.x) + (y * rhs.y);
}

void Vector2::operator+= (const Vector2& rhs)
{
	x += rhs.x;
	y += rhs.y;
}

void Vector2::operator-= (const Vector2& rhs)
{
	x -= rhs.x;
	x -= rhs.y;
}

Vector2& Vector2::operator*= (const float fScalar)
{
	x *= fScalar;
	y *= fScalar;

	return *this;
}

Vector2& Vector2::operator*= (const Vector2& rhs)
{
	x *= rhs.x;
	y *= rhs.y;

	return *this;
}

void Vector2::chamfer()
{
	int x = static_cast<int>(this->x);
	int y = static_cast<int>(this->y);
	this->x = static_cast<float>(x);
	this->y = static_cast<float>(y);
}